package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Service;
import com.study.model.pojo.User;
import com.study.services.CareService;
import com.study.services.CustomerService;
import com.study.services.DictionaryService;
import com.study.services.LinkmanService;
import com.study.services.ServiceSerivce;
import com.study.services.UserService;

@Controller
public class CareController {
	@Autowired
	CareService service;
	
	@GetMapping("/caretys")
	public ModelAndView getForm5() {
		ModelAndView mv = new ModelAndView("afterSale/kehuguanhuai");
		//service.isAnn(1);
		return mv;
	}

	/**
	 * 查看服务（第一次进入）
	 */
	@RequestMapping("/carety")
	@ResponseBody
	public String service1(HttpSession session) {
		Integer cusid = (Integer) session.getAttribute("cusview");
		List<JSONObject> list = service.selectBySel(1, 6, "", "",cusid);
		// pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		session.removeAttribute("cusview");
		return json;
	}
	
	@GetMapping("carety2")
	public ModelAndView getForm2() {
		ModelAndView mv = new ModelAndView("/afterSale/addcare");
		mv.addObject("service",new JSONObject());
		return mv;
	}
	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("afterSale/kehuguanhuai")
	@ResponseBody
	public String getForm3(String input, String selects, int currentPage, int pagesize) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(input);
		System.out.println(selects);

		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(), selects.trim(),0);
		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		
		
        String json = JSONObject.toJSONString(pager);
        
        
		return json;
	}
	
	
	/**
	 * 加载“新建”的iframe界面 ty
	 * 
	 * @return
	 */
	@GetMapping("careadd")
	public ModelAndView getForm() {
		ModelAndView mv = new ModelAndView("/afterSale/addcare");
		mv.addObject("care",new JSONObject());
		return mv;
	}
	/**
	 * 添加
	 */
	@PostMapping("careform")
	@ResponseBody
	public boolean handleForm(String theme, Integer custid,Integer linId, Date dates,
							Integer type,String content,String back,String more,HttpSession session) {
		System.out.println(dates+"111111111");
		
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = service.addCare(theme,custid,linId,dates,useid,type,content,back,more);	
		System.out.println(result+"我是客户关怀的结果");
		return result;
	}

	
	/**
	 * 编辑
	 */
	@GetMapping("carEditor")
	public ModelAndView addCustomerByColle(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		JSONObject care = service.selectBycid(parameter);
		ModelAndView mv = new ModelAndView("/afterSale/addcare");
		mv.addObject("care", care);
		System.out.println(service+"我是一条关怀");
		return mv;
	}
	

	/**
	 * 编辑
	 * @param name
	 * @param custname
	 * @param type
	 * @param mode
	 * @param date1
	 * @param date2
	 * @param spendtime
	 * @param linkman
	 * @param state
	 * @param user
	 * @param content
	 * @param back
	 * @param more
	 * @return
	 */
	@PostMapping("editercareform")
	@ResponseBody
	public boolean editorForm(String theme, Integer custid,Integer linId, Date dates,
			Integer type,String content,String back,String more,Integer id,HttpSession session) {
		System.out.println(custid+"编辑服务"+linId);
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = service.editorService(theme,custid,linId,dates,useid,type,content,back,more,id);
		System.out.println(result+"我是售关怀的结果");
		return result;
	}
	
	
	/**
	 * 删除关怀
	 * @param goo_id
	 * @return
	 */
	@PostMapping("/delCare")
	@ResponseBody
	public String delete(Integer id) {
		System.out.println(id+"删除关怀的id");
		service.delete(id);
		return "ok";
	}
	
	/**
	 * 查询当天的关怀
	 */
	@GetMapping("/careDay")
	public ModelAndView findCareByDay(HttpSession session) {
		Integer id = (Integer) session.getAttribute("useid");
		ModelAndView mv = new ModelAndView("/careMsg");
		List<JSONObject> list = service.findCareByDay(id);
		mv.addObject("careList", list);
		return mv;
	}
	
	/**
	 * 修改关怀的car_type+isdo
	 */
	@PostMapping("/careChange")
	@ResponseBody
	public boolean careChange(Integer type,Integer id,Integer cusid,String theme,HttpSession session) {
		Integer useid = (Integer) session.getAttribute("useid");
		String username = (String) session.getAttribute("username");
		boolean reslut = service.careChange(type,id,useid,cusid,username,theme);
		return reslut;
	}
	
	
}
